﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using DevEnverModal;
using QuailDashboard.View.Models;
using QuailDashboard.View.QuailTestRunnerServiceReference;
using QuailDashboard.View.Views;

namespace QuailDashboard.View.Controls
{
    public partial class SelectBinaryPackageControl
    {
        private readonly IsProcessingStateModel _pageState = new IsProcessingStateModel();

        public SelectBinaryPackageControl()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Gets or sets the ModalControl manager for this instance
        /// </summary>
        public ModalControl Modal
        {
            get;
            set;
        }

        public BinaryPackageInfo SelectedBinaryPackage
        {
            get
            {
                return ListPackages.SelectedItem as BinaryPackageInfo;
            }
        }

        public TestRunOptions Options
        {
            get; set;
        }

        public ObservableCollection<BinaryPackageInfo> Packages
        {
            get; set;
        }

        public event EventHandler Select;

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = _pageState;
            ListPackages.ItemsSource = Packages;
        }


        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedBinaryPackage == null)
            {
                ChildWindow errorWin = new ErrorWindow("No Package Selected", "Binary package is required.");
                errorWin.Show();
            }
            else
            {
                if (Options.CustomOverrides == null) Options.CustomOverrides = new Dictionary<string, string>();
                //if (!String.IsNullOrEmpty(NumberOfTests.Text))
                //    Options.CustomOverrides.Add("NumberOfTests", NumberOfTests.Text);
                //if (!String.IsNullOrEmpty(NumberOfThreads.Text))
                //    Options.CustomOverrides.Add("NumberOfThreads", NumberOfThreads.Text);


                if (Select != null)
                    Select(this, EventArgs.Empty);
                Modal.HideModal();
            }
        }


        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Modal.HideModal();
        }
    }
}
